package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.CommissionSchema;
import org.hgvc.webapp.ets.bo.CommissionSchemaMembership;
import org.hgvc.webapp.ets.bo.CommissionSchemaPaymentRange;
import org.hgvc.webapp.ets.bo.Membership;
import org.hgvc.webapp.ets.constant.AgentConstant;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.BeanUtil;
import org.jocsb.base.util.SpringUtils;


public class AdminCommSchBean extends AbstractBean {

	public static final String BEAN_NAME="adminCommSchBean";
	public static final String NAV_LIST="adminCommSchList";
	public static final String NAV_CREATION="adminCommSchCreate";
	public static final String NAV_EDIT="adminCommSchEdit";
	private AgentService service;
	private CommissionSchema schema;
	private CommissionSchemaPaymentRange range;
	private List<CommissionSchema> schemaList;
	private List<CommissionSchemaPaymentRange> rangeList;
	private List<SelectItem> accumulateType;
	private List<SelectItem> commissionType;
	private List<SelectItem> valueType;
	private List<SelectItem> membershipList;
	private String membershipId;
	private String catType;
	private boolean flag;
	private String test;
	private boolean check = true;
	private List<CommissionSchemaMembership> commMembershipList;



	public void setCommMembershipList(
			List<CommissionSchemaMembership> commMembershipList) {
		this.commMembershipList = commMembershipList;
	}
	public CommissionSchema getSchema() {
		return schema;
	}
	public void setSchema(CommissionSchema schema) {
		this.schema = schema;
	}
	public void setService(AgentService service) {
		this.service = service;
	}
	public CommissionSchemaPaymentRange getRange() {
		return range;
	}
	public void setRange(CommissionSchemaPaymentRange range) {
		this.range = range;

	}
	public List<CommissionSchemaPaymentRange> getRangeList() {
		return rangeList;
	}
	public void setRangeList(List<CommissionSchemaPaymentRange> rangeList) {
		this.rangeList = rangeList;
	}
	public String getMembershipId() {
		return membershipId;
	}
	public void setMembershipId(String membershipId) {
		this.membershipId = membershipId;
	}
	public String getCatType() {
		return catType;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	public String getTest() {
		return test;
	}
	public void setTest(String test) {
		this.test = test;
	}
	public boolean isCheck() {
		return check;
	}
	public void setCheck(boolean check) {
		this.check = check;
	}
	/*---------------------------------------------------------------------------*/
	public List<CommissionSchemaMembership> getCommMembershipList() {
		try{
			if(commMembershipList.size()==0){
				
				List<Membership> mList = service.getMembershipList();
				for(Membership membership:mList){
					CommissionSchemaMembership csm = new CommissionSchemaMembership();
					csm.setMembership(membership);
					commMembershipList.add(csm);
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}

		return commMembershipList;
	}
	public void setCatType(String catType) {
		if(catType.equalsIgnoreCase(AgentConstant.AGENT_COMM_SCH_COMM_TYPE_CAT)){
			flag = true;
		}else{
			flag = false;
		}
		this.catType = catType;
	}
	public List<SelectItem> getAccumulateType() {
		accumulateType = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(AgentConstant.AGENT_COMM_SCH_ACC_TYPE_ACC
				,SpringUtils.getMessage(AgentConstant.AGENT_COMM_SCH_ACC_TYPE_ACC, new Object[]{}));
		SelectItem item2 = new SelectItem(AgentConstant.AGENT_COMM_SCH_ACC_TYPE_NON_ACC
				,SpringUtils.getMessage(AgentConstant.AGENT_COMM_SCH_ACC_TYPE_NON_ACC, new Object[]{}));
		accumulateType.add(item1);
		accumulateType.add(item2);
		return accumulateType;
	}
	public List<SelectItem> getCommissionType() {
		commissionType = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(AgentConstant.AGENT_COMM_SCH_COMM_TYPE_CAT
				,SpringUtils.getMessage(AgentConstant.AGENT_COMM_SCH_COMM_TYPE_CAT, new Object[]{}));
		SelectItem item2 = new SelectItem(AgentConstant.AGENT_COMM_SCH_COMM_TYPE_RANGE
				,SpringUtils.getMessage(AgentConstant.AGENT_COMM_SCH_COMM_TYPE_RANGE, new Object[]{}));
		commissionType.add(item1);
		commissionType.add(item2);
		return commissionType;
	}
	public List<SelectItem> getValueType() {
		valueType = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(AgentConstant.AGENT_COMM_SCH_VAL_TYPE_VAL
				,SpringUtils.getMessage(AgentConstant.AGENT_COMM_SCH_VAL_TYPE_VAL, new Object[]{}));
		SelectItem item2 = new SelectItem(AgentConstant.AGENT_COMM_SCH_VAL_TYPE_PERCENT
				,SpringUtils.getMessage(AgentConstant.AGENT_COMM_SCH_VAL_TYPE_PERCENT, new Object[]{}));
		valueType.add(item1);
		valueType.add(item2);
		return valueType;
	}
	public List<SelectItem> getMembershipList() {
		try{
			List<Membership> mList = service.getMembershipList();
			membershipList = new ArrayList<SelectItem>();
			for(Membership mem:mList){
				SelectItem item = new SelectItem(mem.getMembershipId(),mem.getCode()+" "+mem.getName());
				membershipList.add(item);
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return membershipList;
	}
	public List<CommissionSchema> getSchemaList() {
		try{
			schemaList = service.getCommissionSchemaListType(AgentConstant.AGENT_COMM_SCH_ADMIN_TYPE);
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return schemaList;
	}

	public String navListAction(){
		return NAV_LIST;
	}

	public String navCreateAction(){
		schema = new CommissionSchema();
		rangeList = new ArrayList<CommissionSchemaPaymentRange>();
		commMembershipList = new ArrayList<CommissionSchemaMembership>();
		catType = AgentConstant.AGENT_COMM_SCH_COMM_TYPE_CAT;
		flag = true;
		return NAV_CREATION;
	}

	public String addMoreCreateAction(){

		if(rangeList == null){
			rangeList = new ArrayList<CommissionSchemaPaymentRange>();
		}
		double t = 0;
		if(range !=null){
			rangeList.remove(range);
			CommissionSchemaPaymentRange keep = new CommissionSchemaPaymentRange();
			BeanUtil.copyFields(range, keep, true);
			keep.setFlag(false);
			rangeList.add(keep);
			t = range.getToDbl();
		}
		range = new CommissionSchemaPaymentRange();
		range.setFlag(true);
		range.setFromDbl(t+1);
		rangeList.add(range);

		return NAV_CREATION;
	}

	public String addMoreUpdateAction(){

		if(rangeList == null){
			rangeList = new ArrayList<CommissionSchemaPaymentRange>();
		}
		double t = 0;
		if(range !=null){
			rangeList.remove(range);
			CommissionSchemaPaymentRange keep = new CommissionSchemaPaymentRange();
			BeanUtil.copyFields(range, keep, true);
			keep.setFlag(false);
			rangeList.add(keep);
			t = range.getToDbl();
		}
		range = new CommissionSchemaPaymentRange();
		range.setFlag(true);
		range.setFromDbl(t+1);
		rangeList.add(range);

		return NAV_EDIT;
	}

	public String removeAction(){
		rangeList.remove(range);
		range = null;
		if(rangeList.size()>0){
			range = rangeList.get(rangeList.size()-1);
			range.setFlag(true);
		}
		return NAV_CREATION;
	}

	public void validator(FacesContext context,UIComponent comp,Object obj){
		String value = (String)obj;
		if(value.equalsIgnoreCase("1")){
			check = false;
		}else{
			check = true;
		}
	}

	public String createAction(){
		try{
			schema.setType(AgentConstant.AGENT_COMM_SCH_ADMIN_TYPE);
			if(membershipId!=null&&membershipId.length()>0)
				schema.setMembership(new Membership(membershipId));


			schema.setCommissionType(catType);
			service.createCommissionSchemaType(schema, getThisUser(),rangeList,commMembershipList);
			return NAV_LIST;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_CREATION;
	}



	public void editActionListener(ActionEvent ae){
		try{
			UIParameter param = (UIParameter)ae.getComponent().findComponent("commId");
			String id = (String)param.getValue();
			for(CommissionSchema s:schemaList){
				if(s.getCommSchId().equalsIgnoreCase(id)){
					schema = s;
					rangeList = service.getCommissionSchemaPaymentRangeBySchId(schema.getCommSchId());
					commMembershipList = service.getCommissionSchemaMembershipBySchId(schema.getCommSchId());
					catType = schema.getCommissionType();
					if(schema.getCommissionType().equalsIgnoreCase(AgentConstant.AGENT_COMM_SCH_COMM_TYPE_CAT)){
						flag = true;
					}else{
						flag = false;
					}
					if(rangeList!=null&&rangeList.size()>0){
						range = rangeList.get(rangeList.size()-1);
						range.setFlag(true);
					}
					break;
				}
			}
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
	}

	public String navEditAction(){
		return NAV_EDIT;
	}

	public String editAction(){
		try{
			if(membershipId!=null&&membershipId.length()>0)
				schema.setMembership(new Membership(membershipId));
			schema.setCommissionType(catType);
			service.editCommissionSchemaType(schema, getThisUser(),rangeList,commMembershipList);
			return NAV_LIST;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_EDIT;
	}

	public String deleteAction(){
		try{
			service.deleteCommissionSchemaType(schemaList, getThisUser());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST;
	}

}
